package spring.web.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;

import spring.web.dto.BoardDto;

public class BoardDaoImpl implements BoardDao {
	
	private JdbcTemplate jdbcTemplate;
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public List getList() {
		
		List result = new ArrayList();
		
		String sql = "select * from board order by seq desc";
		
		RowMapper mapper = new RowMapper() {

			@Override
			public Object mapRow(ResultSet rs, int arg1) throws SQLException {
				BoardDto dto = new BoardDto();
				dto.setSEQ(rs.getInt("SEQ"));
				dto.setTITLE(rs.getString("TITLE"));
				dto.setCONTENT(rs.getString("CONTENT"));
				dto.setWRITER(rs.getString("WRITER"));
				dto.setHITCOUNT(rs.getInt("HITCOUNT"));
				dto.setREGDATE(rs.getString("REGDATE"));
				dto.setPASSWORD(rs.getString("PASSWORD"));
				return dto;
			}
			
		};
		
		result = jdbcTemplate.query(sql , mapper);
		
		return result;
	}

	@Override
	public BoardDto getView(int seq) {
		String sql = "select * from board where seq = ?";
		
		Object[] value = { new Integer(seq) };
		
		RowMapper mapper = new RowMapper() {

			@Override
			public Object mapRow(ResultSet rs, int arg1) throws SQLException {
				BoardDto dto = new BoardDto();
				dto.setSEQ(rs.getInt("SEQ"));
				dto.setTITLE(rs.getString("TITLE"));
				dto.setCONTENT(rs.getString("CONTENT"));
				dto.setWRITER(rs.getString("WRITER"));
				dto.setHITCOUNT(rs.getInt("HITCOUNT"));
				dto.setREGDATE(rs.getString("REGDATE"));
				dto.setPASSWORD(rs.getString("PASSWORD"));
				return dto;
			}
			
		};
		
		return (BoardDto)jdbcTemplate.queryForObject(sql , value , mapper);
	}

	@Override
	public void doUpdate(BoardDto dto) {
		String sql = "update board set title = ? , content = ? where seq = ?";
		
		Object[] value = { 
				dto.getTITLE() , dto.getCONTENT() , dto.getSEQ() 
		};
		
		jdbcTemplate.update(sql , value);
	}

	@Override
	public void doInsert(BoardDto dto) {
		
		Calendar cal = Calendar.getInstance();
		String now_DateTime = String.format("%04d%02d%02d", cal.get(Calendar.YEAR) , "-" , cal.get(Calendar.MONTH)+1 , "-" , cal.get(Calendar.DATE));
		
		String sql = "insert into board(title , content , writer , regdate , password , hitcount) " +
		"values(? , ? , ? , '" + now_DateTime + "', ?, 0)";

		Object[] values = new Object[]{
				dto.getTITLE(), dto.getCONTENT(), dto.getWRITER(), dto.getPASSWORD()
		};
		
		jdbcTemplate.update(sql, values);
	}

	@Override
	public void doDelete(int seq) {
		String sql = "delete from board where seq = ?";
		
		Object[] values = new Object[]{
			seq	
		};
		
		jdbcTemplate.update(sql, values);
	}
	
}
